<template>
  <div class="app-page">
    <!-- 标题 -->
    <div class="app-header">
      <div class="app-title">
        <i :class="$route.meta.icon"></i>  {{ $route.meta.title }}
      </div>
      <div class="app-tools">
        <el-button icon="fa fa-plus" size="small" @click="goRouterView('/add')">
          添加
        </el-button>
        <el-button :disabled="multipleSelection.length===0" icon="el-icon-delete" size="small" @click="delRows(multipleSelection)">
          批量删除
        </el-button>
      </div>
    </div>
    <!-- 内容 -->
    <div class="app-body">
      <!-- 表格 -->
      <div v-auto-height class="app-table-box">
        <el-table
          v-loading="tableLoading"
          :data="tableData"
          border
          height="100%"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <!-- <el-table-column type="index" width="70" align="center" label="序号" /> -->
          <el-table-column prop="field01" align="center" label="项目名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="field02" align="center" label="类型" show-overflow-tooltip></el-table-column>
          <el-table-column prop="field03" align="center" label="计划开始时间" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.field03 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" class="primary-color" size="medium" @click="goRouterView(`/edit?uuid=${scope.row.uuid}`)">
                编辑
              </el-button>
              <el-button type="text" class="danger-color" size="medium" @click="delRows([scope.row.uuid])">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="pagination.current"
        :page-sizes="pagination.sizes"
        :page-size="pagination.size"
        :total="pagination.total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChange"
        @current-change="pageChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { remove, getData } from '@/mock/index'

// 搜索分页表
import tablePaginationMixin from '@/mixins/tablePagination.js'
export default {
  name: 'ExampleTablePagination',
  mixins: [tablePaginationMixin],
  data () {
    return {
      // 多选
      multipleSelection: []
    }
  },
  created () {
    this.getTableData()
  },
  methods: {
    // 选择变化
    handleSelectionChange (val) {
      this.multipleSelection = val.map(ele => ele.uuid)
    },
    // 获取列表
    async getTableData () {
      // 模拟表格数据
      const tableData = {
        current: 1,
        pages: 1,
        records: [
          {
            field01: '项目A',
            field02: '',
            field03: '2019-3-12',
            field04: '论证阶段',
            field05: '210000',
            field06: '',
            uuid: 'uuid'
          }
        ],
        size: 20,
        total: 1
      }
      const { current, size } = this.pagination
      this.tableLoading = true
      const res = await getData({ current, size }, tableData)
      this.tableLoading = false
      if (res.code === '0000') {
        const { current, size, total, records } = res.data
        // console.log(res)
        this.pagination = Object.assign({}, this.pagination, { current, size, total })
        this.tableData = records
      }
    },
    // 删除
    delRows (ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ ids })
        if (res.code === '0000') {
          this.$message({
            type: 'success',
            message: res.message
          })
          // 删除最后一个时处理
          if (this.tableData.length === ids.length && this.pagination.current > 1) {
            this.pagination.current = this.pagination.current - 1
          }
          // 更新表格
          this.getTableData()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
